package cn.mxh.pethspt.service.impl;

import cn.mxh.pethspt.dao.CombinationDao;
import cn.mxh.pethspt.dao.impl.CombinationDaoImpl;
import cn.mxh.pethspt.entity.Combination;
import cn.mxh.pethspt.entity.Ding;
import cn.mxh.pethspt.service.CombinationService;
import cn.mxh.pethspt.utils.DruidUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class CombinationServiceImpl implements CombinationService {
        CombinationDao combinationDao = new CombinationDaoImpl();
    /**
     * 添加套餐
     *
     * @param combination
     * @return
     */
    @Override
    public boolean add(Combination combination) throws Exception {
        Connection connection = null;
        int num = 0;
        try{
            connection = DruidUtils.getConnection();
            connection.setAutoCommit(false);// 关闭事物控制
            num = combinationDao.add(combination);
            if(num == 1){// 新增订单总表成功
                connection.commit();// 提交
            }
        } catch (Exception e){
            connection.rollback();// 不提交
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return (num == 1);
    }

    /**
     * 修改套餐
     *
     * @param combination
     * @return
     */
    @Override
    public boolean update(Combination combination) throws Exception {
        Connection connection = null;
        int num = 0;
        try{
            connection = DruidUtils.getConnection();
            connection.setAutoCommit(false);// 关闭事物控制
            num = combinationDao.update(combination);
            if(num == 1){// 新增订单总表成功
                connection.commit();// 提交
            }
        } catch (Exception e){
            connection.rollback();// 不提交
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return (num == 1);
    }

    /**
     * 根据id删除套餐
     *
     * @param coId@return
     * @throws Exception
     */
    @Override
    public boolean delete(Integer coId) throws Exception {
        Connection connection = null;
        int num = 0;
        try{
            connection = DruidUtils.getConnection();
            connection.setAutoCommit(false);// 关闭事物控制
            num = combinationDao.delete(coId);
            if(num == 1){// 新增订单总表成功
                connection.commit();// 提交
            }
        } catch (Exception e){
            connection.rollback();// 不提交
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return (num == 1);
    }

    /**
     * 查询所有套餐
     *
     * @return
     */
    @Override
    public List<Combination> getAll() throws Exception {
        Connection conn = null;
        List<Combination> combinationList = new ArrayList<>();
        try{
            conn = DruidUtils.getConnection();
            combinationList = combinationDao.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DruidUtils.close(conn);
        }
        return combinationList;
    }

    /**
     * 根据id查询套餐价格
     *
     * @param coId@return
     */
    @Override
    public Combination getCombinationBycoId(Integer coId) throws Exception {
        Connection conn = null;
        Combination combination = null;
        try{
            conn = DruidUtils.getConnection();
            combination = combinationDao.getCombinationBycoId(coId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DruidUtils.close(conn);
        }
        return combination;
    }
}
